﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Utility
{
    /// <summary>
    /// StringToConvert 字符串的格式转换
    /// </summary>
    public class StringToConvert
    {
        public StringToConvert()
        {
        }

        public static string StringToPrice(string sPrice)
        {
            string Price = "";
            string[] Strs = sPrice.Split('.');

            string sLeft = Strs[0];             //小数点左边的数
            string sRight = "";                 //小数点右边的数
            try
            {
                sRight = Strs[1];
            }
            catch
            {
            }

            //处理小数点左边
            int i = 0;                          //临时变量
            int len = (sLeft.Length) / 3 + 1;   //字符串有多少个单位长
            string TempPrice = "";

            while (i < len)
            {
                if ((sLeft.Length - 3) >= 0)
                {
                    if ((sLeft.Length - 3) != 0)
                    {
                        TempPrice = "," + Right(sLeft, 3);
                    }
                    else
                    {
                        TempPrice = Right(sLeft, 3);
                    }

                    Price = TempPrice + Price;
                    sLeft = sLeft.Substring(0, (sLeft.Length - 3));                  
                }
                else
                {
                    Price = sLeft + Price;
                }

                i++;
            }

            //处理小数点右边
            if (!sRight.Equals(""))
            {
                Price += sRight;
            }
            else
            {
                Price += ".00";
            }

            return Price;
        }

        /// <summary>
        /// 字符串从左开始取，取固定长度
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iLength">要取的长度</param>
        /// <returns></returns>
        public static string Left(string sSource,int iLength)
        {
            return sSource.Substring(0,iLength>sSource.Length?sSource.Length:iLength);              
        }

        /// <summary>
        /// 字符串从右开始取，取固定长度
        /// </summary>
        public static string Right(string sSource, int iLength)
        {
            return sSource.Substring(iLength > sSource.Length ? 0 : sSource.Length - iLength);
        }

        /// <summary>
        /// 字符串截取固定长度
        /// </summary>
        public static string Mid(string sSource, int iStart, int iLength)
        {
            int iStartPoint = iStart > sSource.Length ? sSource.Length : iStart;
            return sSource.Substring(iStartPoint, iStartPoint + iLength > sSource.Length ? sSource.Length - iStartPoint : iLength);
        }
    }

}
